package leetcode_1_200;

public class LeeCode_112 {
    public static void main(String[] args) {
        System.out.println(hasPathSum(new TreeNode(1, new TreeNode(2), new TreeNode(3)), 4));
    }

    private static boolean hasPathSum(TreeNode root, int targetSum) {
        return search(root, 0, targetSum);
    }

    private static boolean search(TreeNode node, int sum, int targetSum){
        if (node == null) {
            return false;
        }
        sum += node.val;
        if (node.left == null && node.right == null){
            return sum == targetSum;
        }
        return search(node.left, sum, targetSum) || search(node.right, sum, targetSum);
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
